<?php
class Home extends Public_Controller
{
	function __construct()
	{
		parent::__construct();
	}
	
	function index($slug = 'main')
	{
		$data['slug'] = $slug;
    	$this->template->set_layout('home');
		$this->template->build('index',$data);
	}
	
	function inc_top($slug = 'main')
	{
		$data['agency'] = new Category();
		$data['categories'] = new Category();
		$id = $data['agency']->get_by_slug($slug)->id;
		$data['categories']->where('parent_id',$id)->where_related_layout('parent_id',1)->get();
		foreach($data['categories'] as $category)
		{
			$this->{$category->layout->name}($category);
		}
	}
	
	function marquee($category)
	{
		echo $category->content->order_by('id','desc')->get()->title;
	}
	
	function inc_side($slug = 'main')
	{
		$data['agency'] = new Category();
		$data['categories'] = new Category();
		$id = $data['agency']->get_by_slug($slug)->id;
		$data['categories']->where('parent_id',$id)->where_related_layout('parent_id',2)->order_by('sequence asc,id asc')->get();
		foreach($data['categories'] as $category)
		{
			$this->{$category->layout->name}($category);
		}
	}
	
	function inc_main($slug = 'main')
	{
		header ('Content-type: text/html; charset=utf-8');
		$data['agency'] = new Category();
		$data['categories'] = new Category();
		$id = $data['agency']->get_by_slug($slug)->id;
		$data['categories']->where('parent_id',$id)->where_related_layout('parent_id',3)->order_by('sequence asc,id asc')->get();
		foreach($data['categories'] as $category)
		{
			$this->{$category->layout->name}($category);
			//echo $category->layout->name.'<br />';
		}
	}
	
	function poll($category)
	{
		echo modules::run('polls/inc_side',$category->parent->slug);
	}
	
	function menu1($category)
	{
		$data['category'] = $category;
		echo $this->load->view('menu1',$data,TRUE);
	}
	
	function menu2($category)
	{
		$data['category'] = $category;
		echo $this->load->view('menu2',$data,TRUE);
	}
	
	function member()
	{
		echo modules::run('users/inc_side');
	}
	
	function calendar($category)
	{
		echo modules::run('calendars/inc_side',$category->parent->slug);
	}
	
	function link($category)
	{
		echo modules::run('weblinks/inc_index',$category->parent->slug);
	}
	
	function counter()
	{
		
	}
	
	function hilight($category)
	{
		$data['category'] = $category;
		$data['contents'] = $data['category']->content->where("start_date <= date(sysdate()) and (end_date >= date(sysdate()) or end_date = date('0000-00-00')) and status = 'approve'")->limit(10)->order_by('id','desc')->get();
		echo $this->load->view('hilight',$data,TRUE);
		
	}
	
	function information($category)
	{
		$data['category'] = $category;
		/*$data['contents'] = $data['category']->content->where("start_date <= date(sysdate()) and (end_date >= date(sysdate()) or end_date = date('0000-00-00')) and status = 'approve'")->limit(4)->order_by('id','desc')->get();*/
		
		$data['contents'] = new Content();
		$data['contents']->where("start_date <= date(sysdate()) 
		and (end_date >= date(sysdate()) 
		or end_date = date('0000-00-00')) 
		and status = 'approve' 
		and category_id in (select id from content_categories where layout_id = 12)")->limit(4)->order_by('start_date','desc')->get();
		
		echo $this->load->view('information',$data,TRUE);
	}
	function tab($parent)
	{
		if($parent->structure->type=='list')
		{
			$data['category'] = $parent;
			$data['contents'] = $parent->content->where("start_date <= date(sysdate()) and (end_date >= date(sysdate()) or end_date = date('0000-00-00')) and status = 'approve'")->limit(4)->order_by('id','desc')->get();
			echo $this->load->view('list',$data,TRUE);
		}
		else
		{
			$data['parent'] = $parent;
			$data['categories'] = new Category();
			$data['categories']->where('parent_id',$parent->id)->order_by('id','asc')->get();
			echo $this->load->view('tab',$data,TRUE);
		}
		
	}
	function list_left($category)
	{
		$data['category'] = $category;
		$group[11] = '11,334,61,62,63';
		if(!empty($group[$category->id]))
		{
			$data['contents'] = new Content();
			$data['contents']->where('category_id in ('.$group[$category->id].')')
			->where("start_date <= date(sysdate()) and (end_date >= date(sysdate()) or end_date = date('0000-00-00')) and status = 'approve'")
			->limit(3)
			->order_by('id','desc')
			->get();
		}
		else
		{
			$data['contents'] = $data['category']->content->where("start_date <= date(sysdate()) and (end_date >= date(sysdate()) or end_date = date('0000-00-00')) and status = 'approve'")->limit(3)->order_by('id','desc')->get();
		}
		
		echo $this->load->view('list_left',$data,TRUE);
	}
	function list_right($category)
	{
		$data['category'] = $category;
		$data['contents'] = $data['category']->content->where("start_date <= date(sysdate()) and (end_date >= date(sysdate()) or end_date = date('0000-00-00')) and status = 'approve'")->limit(3)->order_by('id','desc')->get();
		echo $this->load->view('list_right',$data,TRUE);
	}
	function album($category)
	{
		echo modules::run('albums/inc_index',$category->parent->slug);
	}
	
	function sitemap($slug='main')
	{
		$data['slug'] = $slug;
		$data['categories'] = new Category();
		$data['categories']->where('parent_id',0)->get();
		$this->template->build('sitemap',$data);
	}
	
	function test()
	{
		$this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
		if ( ! $foo = $this->cache->get('foo'))
		{
		     echo 'Saving to the cache!<br />';
		     $foo = 'foobarbaz!';
		
		     // Save into the cache for 5 minutes
		     $this->cache->save('foo', $foo, 300);
		}
		echo $foo; 
	}
}
?>